import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../data/models/device_model.dart';
import '../../../constants/enums/device_type.dart';
import '../../../constants/enums/device_status.dart';
import './device_status_badge.dart'; // 需要创建这个Widget

/// 设备卡片 Widget
class DeviceCard extends StatelessWidget {
  final DeviceModel device;
  final VoidCallback? onTap;
  final ValueChanged<bool>? onSwitchChanged; // 用于可控制设备的开关

  const DeviceCard({
    Key? key,
    required this.device,
    this.onTap,
    this.onSwitchChanged,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final bool isControllable = device.type.isController;

    return Card(
      elevation: 1.5,
      margin: const EdgeInsets.symmetric(vertical: 6.0, horizontal: 8.0),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(10.0),
        child: Padding(
          padding: const EdgeInsets.all(12.0),
          child: Row(
            children: [
              // 设备图标
              _buildDeviceIcon(context),
              
              const SizedBox(width: 16),
              
              // 设备信息
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      device.name,
                      style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 4),
                    Text(
                      device.location ?? '未知位置',
                      style: TextStyle(fontSize: 12, color: Colors.grey[600]),
                    ),
                    const SizedBox(height: 6),
                    // 设备类型和状态标签
                    Row(
                      children: [
                        DeviceStatusBadge(status: device.status),
                        const SizedBox(width: 8),
                        Text(
                          device.type.displayName, // 使用扩展获取显示名称
                          style: TextStyle(fontSize: 11, color: Colors.grey[700]),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              
              // 控制开关 (如果设备可控)
              if (isControllable && onSwitchChanged != null)
                _buildControlSwitch(),
                
              // 或显示数值 (传感器等)
              if (!isControllable && device.properties != null)
                 _buildDeviceValue(context),
              
            ],
          ),
        ),
      ),
    );
  }

  /// 构建设备图标
  Widget _buildDeviceIcon(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(10),
      decoration: BoxDecoration(
        color: Theme.of(context).primaryColor.withOpacity(0.1),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Icon(
        device.type.iconData, // 使用扩展获取图标
        size: 28,
        color: Theme.of(context).primaryColor,
      ),
    );
  }

  /// 构建控制开关
  Widget _buildControlSwitch() {
    // 这里假设设备模型中有一个 `isOn` 属性或通过 properties 获取
    bool isOn = device.properties?['power'] == 'on'; // 示例
    
    return Switch(
      value: isOn,
      onChanged: onSwitchChanged,
      activeColor: Get.theme.primaryColor,
    );
  }
  
  /// 构建设备数值显示
  Widget _buildDeviceValue(BuildContext context) {
     // 尝试获取关键属性值，例如温度、湿度等
     String displayValue = '--';
     String unit = '';
     
     switch (device.type) {
       case DeviceType.temperatureSensor:
         displayValue = device.properties?['temperature']?.toString() ?? '--';
         unit = '°C';
         break;
       case DeviceType.humiditySensor:
         displayValue = device.properties?['humidity']?.toString() ?? '--';
         unit = '%';
         break;
       // 添加其他传感器类型的处理
       default:
         // 尝试获取通用的 'value' 属性
         displayValue = device.properties?['value']?.toString() ?? '--';
     }
     
     return Text(
       '$displayValue$unit',
       style: TextStyle(
         fontSize: 16,
         fontWeight: FontWeight.bold,
         color: Theme.of(context).primaryColor,
       ),
     );
  }
} 